package model.service.threads;

import java.sql.Connection;
import java.util.List;

import controller.main.Constants;
import controller.main.Executer;
import model.DbAccess.ConnectionManager;
import model.dto.Location;
import model.service.ChubJobs;
import model.service.FtpController;

public class CountryThread implements Runnable{

	
	String operation ;
	String country;
	List interfaces;
	
	public CountryThread(String o , String c , List i)
	{
		this.operation= o;
		this.country= c;
		this.interfaces= i;
	}
	
	
	public void run() 
	{

	// 1- get a connection from connectionManger
	// 2- if else if on operation, with all possible cases.
   // 3- call FTPFiles , ProcessFilesAtDatabase , SendNotificationMails
   // 4- close the connection for this thread.
		
	//	ConnectionManager connMan=new ConnectionManager();
		//Connection connection=connMan.getConnection();
		ChubJobs chubJobs = new ChubJobs();
		
		try
		{
		
			if(operation.equals(Constants.FTP))
			{
				chubJobs.FTPFiles(country, interfaces);
			}
			else if(operation.equals(Constants.PROCESS))
			{
				chubJobs.ProcessFilesAtDatabase(country, interfaces);
			}
			else if(operation.equals(Constants.MAIL))
			{
				chubJobs.SendNotificationMails(country, interfaces);
			}
			else if(operation.equals(Constants.FTP_PROCESS))
			{
				chubJobs.FTPFiles(country, interfaces);
				chubJobs.ProcessFilesAtDatabase(country, interfaces);		
			}
			else if(operation.equals(Constants.PROCESS_MAIL))
			{
				chubJobs.ProcessFilesAtDatabase(country, interfaces);
				chubJobs.SendNotificationMails(country, interfaces);
			}
			else if(operation.equals(Constants.ALL))
			{
				chubJobs.FTPFiles(country, interfaces);
				chubJobs.ProcessFilesAtDatabase(country, interfaces);	
				chubJobs.SendNotificationMails(country, interfaces);
			}
			
		}
		catch(Exception e)
		{
			//ConnectionManager.logCalller( null, null, null, country, "FTP can't start");
			System.out.println("Thread ended with exceptions for country " +country + " and operation " + operation);
			e.printStackTrace();
		}
		finally
		{
		//connMan.closeConnection(connection);
			
		}
		
		
	}
	
}
